package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.service.PlanWorkOrderService;
import com.uwlaser.service.dto.PlanWorkOrderDto;
import com.uwlaser.service.dto.PlanWorkOrderQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工单管理管理")
@RequestMapping("/api/planWorkOrder")
public class PlanWorkOrderController {

    private final PlanWorkOrderService planWorkOrderService;

    @GetMapping
    @Log("查询工单管理")
    @ApiOperation("查询工单管理")
    @PreAuthorize("@el.check('planWorkOrder:list')")
    public ResponseEntity query(PlanWorkOrderQueryParam query, Pageable pageable){
        return new ResponseEntity<>(planWorkOrderService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/getStatusCount")
    @Log("查询工单不同状态数量")
    @ApiOperation("查询工单不同状态数量")
    @PreAuthorize("@el.check('planWorkOrder:list')")
    public ResponseEntity getStatusCount(){
        return new ResponseEntity<>(planWorkOrderService.getStatusCount(),HttpStatus.OK);
    }

    @GetMapping("/monitor")
    @Log("工单监控查询计划工单列表")
    @ApiOperation("工单监控查询计划工单列表")
    @PreAuthorize("@el.check('planWorkOrder:list')")
    public ResponseEntity monitor(PlanWorkOrderQueryParam query, Pageable pageable){
        return new ResponseEntity<>(planWorkOrderService.monitor(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/failInfo")
    @Log("获取工单列表不合格信息")
    @ApiOperation("获取工单列表不合格信息")
    @PreAuthorize("@el.check('planWorkOrder:list')")
    public ResponseEntity fail(PlanWorkOrderQueryParam query){

        return new ResponseEntity<>(planWorkOrderService.failInfo(query),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工单管理")
    @ApiOperation("新增工单管理")
    @PreAuthorize("@el.check('planWorkOrder:add')")
    public ResponseEntity create(@Validated @RequestBody PlanWorkOrderDto resources){
        return new ResponseEntity<>(planWorkOrderService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工单管理")
    @ApiOperation("修改工单管理")
    @PreAuthorize("@el.check('planWorkOrder:edit')")
    public ResponseEntity update(@Validated @RequestBody PlanWorkOrderDto resources){
        planWorkOrderService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工单管理")
    @ApiOperation("删除工单管理")
    @PreAuthorize("@el.check('planWorkOrder:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        planWorkOrderService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出工单管理")
    @ApiOperation("导出工单管理")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('planWorkOrder:list')")
    public void download(HttpServletResponse response, PlanWorkOrderQueryParam query) throws IOException {
        planWorkOrderService.download(planWorkOrderService.queryAll(query), response);
    }*/

}
